package com.medicalsystem.service;

import com.medicalsystem.entity.Doctor;
import com.medicalsystem.repository.DoctorRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DoctorService {

    private final DoctorRepository doctorRepository;

    public DoctorService(DoctorRepository doctorRepository) {
        this.doctorRepository = doctorRepository;
    }

    // 获取所有医生信息
    public List<Doctor> getAllDoctors() {
        return doctorRepository.findAll();
    }

    // 根据ID获取医生信息
    public Doctor getDoctorById(Long id) {
        return doctorRepository.findById(id).orElse(null);
    }

    // 新增医生信息
    public Doctor saveDoctor(Doctor doctor) {
        return doctorRepository.save(doctor);
    }

    // 修改医生信息
    public Doctor updateDoctor(Long id, Doctor updatedDoctor) {
        Optional<Doctor> existingDoctor = doctorRepository.findById(id);
        if (existingDoctor.isPresent()) {
            Doctor doctor = existingDoctor.get();
            doctor.setDoctorName(updatedDoctor.getDoctorName());
            doctor.setGender(updatedDoctor.getGender());
            doctor.setAge(updatedDoctor.getAge());
            doctor.setIdCard(updatedDoctor.getIdCard());
            doctor.setPhone(updatedDoctor.getPhone());
            doctor.setDepartment(updatedDoctor.getDepartment());
            doctor.setTitle(updatedDoctor.getTitle());
            doctor.setSpecialty(updatedDoctor.getSpecialty());
            doctor.setIntroduction(updatedDoctor.getIntroduction());
            doctor.setUpdateTime(updatedDoctor.getUpdateTime());
            doctor.setRemark(updatedDoctor.getRemark());
            return doctorRepository.save(doctor);
        }
        return null;
    }

    // 根据ID删除医生信息
    public void deleteDoctor(Long id) {
        doctorRepository.deleteById(id);
    }
}
